﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace _2011DBProject
{
    public partial class Gameroom : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null || Session["SERVER"] == null || Session["Ch"] == null || Session["CHARACTER_NAME"] == null)
            {
                Response.Redirect("default.aspx?isNoSession=1");
            }
            else
            {   
                
                lblCharacterName.Text = (Session["CHARACTER_NAME"].ToString());
                if (Settings.strGuildName != "")
                    lblGuild.Text = Settings.strGuildName;

                Label templbl = (Label)(DataListFindServerName.Controls[0].Controls[1]).FindControl("SERVER_NAMELabel");
                lblServer.Text = templbl.Text;

                templbl = (Label)(DataListFindChName.Controls[0].Controls[2]).FindControl("CHANNEL_NAMELabel");
                lblChannel.Text = templbl.Text;

                templbl = (Label)(DataListCharacter.Controls[0].Controls[6]).FindControl("LVLLabel");
                lblLevel.Text = templbl.Text;

                templbl = (Label)(DataListCharacter.Controls[0].Controls[16]).FindControl("NG_AMOUNTLabel");
                lblEnge.Text = templbl.Text;

                templbl = (Label)(DataListCharacter.Controls[0].Controls[19]).FindControl("MODEL_IDLabel");
                ImageButton1.ImageUrl = "image/" + templbl.Text + ".png";
                lblModel.Text = templbl.Text;

                templbl = (Label)(DataListCharacter.Controls[0].Controls[10]).FindControl("EXP_TO_NEXT_LEVELLabel");
                lblExp.Text = templbl.Text;
            }
            //lblCharacterName.Text = HttpContext.Current.Session["CHARACTER_NAME"].ToString();
            //DataSet1.CHARACTERDataTable dsc = new DataSet1.CHARACTERDataTable();
            //DataSet1TableAdapters.CHARACTERTableAdapter dsca = new DataSet1TableAdapters.CHARACTERTableAdapter();
            //dsca.Connection.ConnectionString = Settings.strConnectionString;
            //if (dsca.FillByServerNoCharacterName(dsc, Settings.decServer, Settings.StrCharacterName)>0)
            //{
            //
            //if (!dsc[0].IsLVLNull())
            //
            //lblCharacterLVL.Text = dsc[0].LVL.ToString();
            //
            //if (!dsc[0].IsNG_AMOUNTNull())
            //
            //lblEnge.Text = dsc[0].NG_AMOUNT.ToString();
            //}
        }
        protected void gvGameRoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                lblHid.Text = gvGameRoom.DataKeys[gvGameRoom.SelectedIndex].Value.ToString();
                Session.Add("GAMEROOM_NO", gvGameRoom.DataKeys[gvGameRoom.SelectedIndex].Value);
                // Settings.decGameroom_No = Convert.ToDecimal(gvGameRoom.DataKeys[gvGameRoom.SelectedIndex].Value.ToString());
                btnEnterRoom.Text = gvGameRoom.DataKeys[gvGameRoom.SelectedIndex].Value + "번방 입장하기!";
                btnEnterRoom.Visible = true;
            }
            catch (Exception ex)
            {
                // todo:
            }
        }
        protected void btnEnterRoom_Click(object sender, EventArgs e)
        {
            exitChannel();
            //Session.Add("GAMEROOM_NO", gvGameRoom.DataKeys[gvGameRoom.SelectedIndex].Value.ToString());
            Session.Add("GAMEROOM_NO", gvGameRoom.DataKeys[gvGameRoom.SelectedIndex].Value);
            //Settings.decGameroom_No = Convert.ToDecimal(gvGameRoom.DataKeys[gvGameRoom.SelectedIndex].Value.ToString());
            enterGameroom();
            Response.Redirect("GamePlay.aspx");
        }
        private static void enterGameroom()
        {
            try
            {
                DataSet1.GAMEROOM_ENTRANCE_STATUSDataTable gmEntrance = new DataSet1.GAMEROOM_ENTRANCE_STATUSDataTable();
                DataSet1TableAdapters.GAMEROOM_ENTRANCE_STATUSTableAdapter taGmEntrance = new _2011DBProject.DataSet1TableAdapters.GAMEROOM_ENTRANCE_STATUSTableAdapter();
                DataSet1.GAMEROOM_ENTRANCE_STATUSRow row = gmEntrance.NewGAMEROOM_ENTRANCE_STATUSRow();
                row.CHANNEL_NO = Settings.decCh;
                row.CHARACTER_NAME = Settings.strCharacterName;
                row.SERVER_NO = Settings.decServer;
                row.GAMEROOM_NO = Settings.decGameroom_No;
                gmEntrance.AddGAMEROOM_ENTRANCE_STATUSRow(row);
                taGmEntrance.Update(gmEntrance);
            }
            catch (Exception)
            {
            }
        }
        private void exitChannel()
        {
            try
            {
                DataSet1.CHANNEL_ENTRANCE_STATUSDataTable chEntrance = new DataSet1.CHANNEL_ENTRANCE_STATUSDataTable();
                DataSet1TableAdapters.CHANNEL_ENTRANCE_STATUSTableAdapter taChEntrance = new _2011DBProject.DataSet1TableAdapters.CHANNEL_ENTRANCE_STATUSTableAdapter();
                if (taChEntrance.FillByServerNoChannelNoCharacterName(chEntrance, Settings.decServer, Settings.decCh, Settings.strCharacterName) > 0)
                {
                    DataSet1.CHANNEL_ENTRANCE_STATUSRow row = chEntrance[0];
                    row.Delete();
                    taChEntrance.Update(chEntrance);
                }
            }
            catch (Exception)
            {
            }
        }
        protected void btnMakeRoom_Click(object sender, EventArgs e)
        {
            Response.Redirect("MakeRoom.aspx");
        }
        protected void btnGuildInfo_Click(object sender, EventArgs e)
        {
            exitChannel();
            Response.Redirect("Guild.aspx");
        }
        protected void btnShop_Click(object sender, EventArgs e)
        {
            exitChannel();
            Response.Redirect("Shop.aspx");
        }
        protected void btnCaddie_Click(object sender, EventArgs e)
        {
            exitChannel();
            Response.Redirect("Caddie.aspx");
        }
        protected void btnInven_Click(object sender, EventArgs e)
        {
            exitChannel();
            Response.Redirect("Inven.aspx");
        }
        protected void btnInven0_Click(object sender, EventArgs e)
        {
            exitChannel();
            Response.Redirect("CharacterInven.aspx");
        }
        protected void btnChat_Click(object sender, EventArgs e)
        {
            enterChatText();
        }
        private void enterChatText()
        {
            try
            {
                DataSet1.CONVERSATIONDataTable conversation = new DataSet1.CONVERSATIONDataTable();
                DataSet1TableAdapters.CONVERSATIONTableAdapter taConversation = new _2011DBProject.DataSet1TableAdapters.CONVERSATIONTableAdapter();
                taConversation.FillBySeverNoChannelNo(conversation, Settings.decServer, Settings.decCh);
                DataSet1.CONVERSATIONRow row = conversation.NewCONVERSATIONRow();
                if (chkGuildChat.Checked && lblGuild.Text != "")
                    row.CHANNEL_GUILD_CAT = lblGuild.Text;
                else
                    row.CHANNEL_GUILD_CAT = "";
                row.CHANNEL_NO = Settings.decCh;
                row.CHARACTER_NAME = Settings.strCharacterName;
                row.CONTENT = txtChat.Text.Trim();
                row.CONVERSATION_DATETIME = DateTime.Now;
                row.CONVERSATION_NO = getNextConversationNo();
                row.SERVER_NO = Settings.decServer;
                row.GAMEROOM_NO = 0;
                conversation.AddCONVERSATIONRow(row);
                taConversation.Update(conversation);
                sqldsConversationCh.DataBind();
                sqldsConversationChGuild.DataBind();
                gvConversationCh.DataBind();
                gvConversationChGuild.DataBind();
                txtChat.Text = "";
                SetFocus(txtChat);
            }
            catch (Exception)
            {
                sqldsConversationCh.DataBind();
                txtChat.Text = "";
                throw;
            }
        }
        private decimal getNextConversationNo()
        {
            decimal r = 1;
            try
            {
                System.Data.OracleClient.OracleConnection cnn = new System.Data.OracleClient.OracleConnection(Settings.strConnectionString);
                System.Data.OracleClient.OracleCommand command = new System.Data.OracleClient.OracleCommand("select count(*) from CONVERSATION where SERVER_NO = '" + Settings.decServer + "' and SERVER_NO = " + Settings.decServer + " and CHANNEL_NO = " + Settings.decCh, cnn);
                cnn.Open();
                r = Convert.ToDecimal(command.ExecuteScalar());
                cnn.Close();
                return r;
            }
            catch (Exception)
            {
                throw;
            }
        }
        protected void gvChannelEntrance_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                lblMember.Visible = true;
                lblMember.Text = gvChannelEntrance.DataKeys[gvChannelEntrance.SelectedIndex].Value.ToString();
            }
            catch (Exception)
            {
                throw;
            }
        }
        protected void btnMessage_Click(object sender, EventArgs e)
        {
            try
            {
                if (lblMember.Text != "선택된 멤버가 없습니다.")
                {
                    DataSet1TableAdapters.MESSAGETableAdapter taMsg = new DataSet1TableAdapters.MESSAGETableAdapter();
                    taMsg.Connection.ConnectionString = Settings.strConnectionString;
                    DataSet1.MESSAGEDataTable tbMsg = taMsg.GetDataByServerNoCharacterName(Settings.decServer, lblMember.Text);
                    DataSet1.MESSAGERow row = tbMsg.NewMESSAGERow();
                    row.CHARACTER_NAME = lblMember.Text;
                    row.CONTENT = txtMessage.Text;
                    row.MESSAGE_DATETIME = System.DateTime.Now;
                    row.SERVER_NO = Settings.decServer;
                    tbMsg.AddMESSAGERow(row);
                    taMsg.Update(tbMsg);
                    sqldsMessage.DataBind();
                    gvMessage.DataBind();
                    txtMessage.Text = "";
                    lblMember.Text = "선택된 멤버가 없습니다.";
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        protected void btnGoLogout_onclick(object sender, EventArgs e)
        {
            Session.Clear();
            exitChannel();
            Response.Redirect("Default.aspx");
        }
        protected void btnGoServer_onclick(object sender, EventArgs e)
        {
            exitChannel();
            Response.Redirect("Server.aspx");
        }
        protected void btnGoCharacter_onclick(object sender, EventArgs e)
        {
            exitChannel();
            Response.Redirect("Character.aspx");
        }
        protected void btnGoChannel_onclick(object sender, EventArgs e)
        {
            exitChannel();
            Response.Redirect("Channel.aspx");
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            DataListCharacter.Visible = !DataListCharacter.Visible;
        }

        protected void btnPark0_Click(object sender, EventArgs e)
        {
            exitChannel();
            Response.Redirect("Bench.aspx");
        }

        protected void chkGuildChat_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (Settings.strGuildName == "")
                {
                    chkGuildChat.Checked = false;
                    return;
                }
                if (chkGuildChat.Checked)
                {
                    lblGuild.Visible = true;
                    gvConversationCh.Visible = false;
                    gvConversationChGuild.Visible = true;
                }
                else
                {
                    lblGuild.Visible = false;
                    gvConversationCh.Visible = true;
                    gvConversationChGuild.Visible = false;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
